﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Deanery.Controller.PersonData
{
    class AddressController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        /// <summary>
        /// This method performs INSERT one record to DB in table Address
        /// PROBLEM: method returns nothing
        /// 0 - region
        /// 1 - type_city
        /// 2 - city
        /// 3 - type_street 
        /// 4 - street_name
        /// 5 - int_index
        /// 6 - nv_house_number
        /// 7 - nv_apartment_number
        /// 8 - nv_notes_adress
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param> 
        public void Insert(ArrayList reccord)
        {
            var cityObj = new CityController();
            var streetObj = new StreetController();

            var obj = new Address
            {
                id_address = Guid.NewGuid(),

                id_city = cityObj.FindGuidByCityName(reccord[0].ToString(), reccord[1].ToString()),
                id_street = streetObj.FindGuidByStreetName(reccord[2].ToString()),

            };
            if (3 < reccord.Count) obj.int_index = (int)reccord[3];
            if (4 < reccord.Count) obj.nv_house_number = reccord[4].ToString();
            if (5 < reccord.Count) obj.nv_apartment_number = reccord[5].ToString();
            if (6 < reccord.Count) obj.nv_notes_adress = reccord[6].ToString();

            _db.Addresses.AddObject(obj);
            _db.SaveChanges();
        }


        /// <summary>
        /// This method for Import
        /// performs INSERT one record to DB in table Address
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="city">guid</param>
        /// <param name="street">guid</param>
        /// <param name="house">string</param>
        /// <param name="apartment">string</param> 
        public void Insert(Guid city, Guid street, string house, string apartment)
        {
            var cityObj = new CityController();
            var streetObj = new StreetController();

            var obj = new Address
            {
                id_address = Guid.NewGuid(),

                id_city = city,
                id_street = street,
                nv_house_number = house,
                nv_apartment_number = apartment

            };

            _db.Addresses.AddObject(obj);
            _db.SaveChanges();
        }

        public Guid FindAddress(Guid city, Guid street, string house, string apartment)
        {
            var obj = (from t in _db.Addresses
                       where
                           t.id_city == city && t.id_street == street && t.nv_house_number == house &&
                           t.nv_apartment_number == apartment
                       select t.id_address).SingleOrDefault();
            return obj;
        }
    }
}
